import { TreeComponent } from './tree.component';
import { Component, OnInit, Input } from '@angular/core';
import { dropAnimation } from '../shared/animation/index';

@Component({
  selector: 'el-tree-item',
  animations: [dropAnimation],
  templateUrl: './tree-item.component.html'
})
export class TreeItemComponent implements OnInit {
  root: TreeComponent = new TreeComponent;
  @Input() indent: number = 0;
  @Input() model: any;

  constructor(root: TreeComponent) {
    this.root = root;
  };

  ngOnInit(): void {
  }


  clickHandle(event: any) {
    event.stopPropagation();
    if (!this.root.expandOnClickNode)
      return this.itemEmitter('click');
    this.updateExpanded();
  }

  iconClickHandle(event:any) {
    event.stopPropagation();
    this.updateExpanded();
  }

  checkHandle(event:any) {
    console.log(event,this.root.disabled);
    if (this.root.disabled)
      return;
    this.root.updateChecked(this.model.label);
    this.root.emitter({
      label: this.model.label,
      treeNodeID: this.model.label,
      action: 'checkbox',
      checked: this.model.checked,
    });
  }

  updateExpanded() {
    var /** @type {?} */ dontUpdateExpanded = this.isLeaf();
    var /** @type {?} */ nextAction = dontUpdateExpanded ? 'click' : (this.model.expanded ? 'close' : 'open');
    this.itemEmitter(nextAction);
    !dontUpdateExpanded && this.root.updateExpanded(this.model.label);
  }

  isLeaf() {
    return !this.model.children || !this.model.children.length;
  }

  itemEmitter(action: any) {
    this.root.emitter({
      label: this.model.label,
      treeNodeID: this.model.label,
      action: action,
      checked: this.model.checked,
    });
  }


}
